import os
import platform
def clear_platform():
    if platform.system() == "Windows":
        os.system("cls")
    else:
        os.system("clear")
# 清屏
clear_platform()
PROJECT_NAME = "LLMSpeech"
from audio_recorder import LIGHT_GREEN, RESET
print(F"{LIGHT_GREEN}{PROJECT_NAME}{RESET} 启动中...")
print("[启动进度]")


from threading import Thread

from queues import queues
from audio_recorder import AudioRecorder
from audio_player import AudioPlayer
from sensevoice_wrapper import SenseVoiceWrapper
from natural_language_processor import NaturalLanguageProcessing
from cosyvoice_wrapper import CosyVoiceWrapper


class LLMSpeech:
    def __init__(self):
        self.threads = []
        self.recorder = AudioRecorder(
            is_recording_event=queues.is_recording_event,
            stt_stopped_event=queues.stt_stopped_event,
            nlp_stopped_event=queues.nlp_stopped_event,
            tts_stopped_event=queues.tts_stopped_event,
            playmusic_stopped_event=queues.playmusic_stopped_event,
            exit_event=queues.exit_event,
            recording_filename_queue=queues.recording_filename_queue,
            playmusic_filename_queue=queues.playmusic_filename_queue,
            playeffect_filename_queue=queues.playeffect_filename_queue,
            enable_print=True,
            project_name=PROJECT_NAME
        )

        self.audio_player = AudioPlayer(
            is_recording_event=queues.is_recording_event,
            playmusic_stopped_event=queues.playmusic_stopped_event,
            exit_event=queues.exit_event,
            playmusic_filename_queue=queues.playmusic_filename_queue,
            playeffect_filename_queue=queues.playeffect_filename_queue
        )

        self.sensevoice_warpper = SenseVoiceWrapper(
            is_recording_event=queues.is_recording_event,
            stt_stopped_event=queues.stt_stopped_event,
            exit_event=queues.exit_event,
            recording_filename_queue=queues.recording_filename_queue,
            stt_text_queue=queues.stt_text_queue,
            show_tqdm=False
        )

        self.nlp = NaturalLanguageProcessing(
            is_recording_event=queues.is_recording_event,
            nlp_stopped_event=queues.nlp_stopped_event,
            exit_event=queues.exit_event,
            stt_text_queue=queues.stt_text_queue,
            tts_text_queue=queues.tts_text_queue,
            playmusic_filename_queue=queues.playmusic_filename_queue,
            playeffect_filename_queue=queues.playeffect_filename_queue,
            clean_markdown=True
        )

        self.cosyvoice_wrapper = CosyVoiceWrapper(
            is_recording_event=queues.is_recording_event,
            tts_stopped_event=queues.tts_stopped_event,
            exit_event=queues.exit_event,
            tts_text_queue=queues.tts_text_queue,
            playmusic_queue=queues.playmusic_filename_queue,
            show_logs=False,
            show_warnings=False,
            show_tqdm=False,
            show_print=False,
            delete_audio=True
        )

    def start(self):
        """原绳，启动！"""
        # 音频录制线程,总控线程
        audio_recorder_thread = Thread(target=self.recorder.start, daemon=True)
        self.threads.append(audio_recorder_thread)

        # 音频播放线程
        audio_player_thread = Thread(target=self.audio_player.start, daemon=True)
        self.threads.append(audio_player_thread)

        # 语音识别线程
        sensevoice_thread = Thread(target=self.sensevoice_warpper.start, daemon=True)
        self.threads.append(sensevoice_thread)

        # 自然语言处理线程
        nlp_thread = Thread(target=self.nlp.start, daemon=True)
        self.threads.append(nlp_thread)

        # 语音合成线程
        cosyvoice_thread = Thread(target=self.cosyvoice_wrapper.start, daemon=True)
        self.threads.append(cosyvoice_thread)
    
        # 启动所有线程
        for thread in self.threads:
            thread.start()

        # 等待所有线程完成
        for thread in self.threads:
            thread.join()

        print(f"\n所有线程均已退出，{LIGHT_GREEN}LLMSpeech{RESET} 退出。再见！\n")

if __name__ == "__main__":
    llm_speech = LLMSpeech()
    llm_speech.start()